#/bin/bash

#get the files under the folder recursively for modified date as the date provided
#GetUpdateFiles /home/test '2013-1-12'
function GetUpdatedFiles
{
	SFolder=$1
	MDate=$2
	for f in $(ls -a $SFolder);
	do 
		#skip clearcase/svn files and skip current folder and above folder
		if [ "$f" = ".svn" ] || [ "$f" = ".copyarea.db" ] ||  [ "$f" = "." ] || [ "$f" = ".." ]  ;
		then
#                        echo "skip $f"
			continue;
		fi
		
		#check if the file is matching the criteria
		if [ -f $f ];
		then
#                        echo "found file $f"
			MSeconds=$(date -d "$MDate" +%s ) 	#get the seconds of given date
			FSeconds=$(stat  -c %Y $f) 		#get the seconds of the file
			if [ $[ $FSeconds - $MSeconds ] -gt 0 -a  $[ $FSeconds - $MSeconds ] -lt 86400 ]
			then
				echo $(pwd)/$f
				if [ bLog == 1 ] ;
				then
					echo $(pwd)/$f>>$LogFile
				fi
			fi
			continue
		fi

		#recursively check down folders
		if [ -d $SFolder/$f ];
		then
#                        echo "enter $(pwd)/$f \"$MDate\";"
			cd $SFolder/$f
			  GetUpdatedFiles .  "$MDate";
			cd ..
			continue
		fi
	done
}


#---------------------- Parse Date Parameter to Date String
bLog=1
SourceFolder=$1
if [ "$2" == "T" ];
then
	DateParam=$(date +%y-%m-%d)
elif [ "$DateParam" == "Y" ]; 
then
	DateParam=$(date -d last-day +%y-%m-%d)
else
	DateParam=$2
fi

#------------------------help information
if [ -z $1 ] || [ -z $2 ] || [ ! -d $1 ];
then
	echo "How to use:"
	echo "		$(basename $0)  'sourcefolder' 'parameter' "
	echo "		 paramater: 'T' for today, 'Y' for yesterday, or Date like '2013-1-20'" 
	exit 0
fi





#------------------------log file
if [ -z $3  ] || [ "$3" != "nolog" ]; # log by default unless name "nolog"
then
	bLog=0
	LogFile=./checkFileUpdateLog
	Num=1
	while [ -f $LogFile$Num ]
	do
		Num=$[ $Num +1]
	done
	LogFile=$LogFile$Num
	echo "create log file $LogFile"
	touch $LogFile #create the log file
	echo "------------------$(date) $(basename $0) " >> $LogFile
fi


#execute the command
#Example GetUpdateFiles /home/test '2013-1-12'
#echo "GetUpdatedFiles $SourceFolder $DateParam start -----------------------------------------"
GetUpdatedFiles $SourceFolder $DateParam
#echo "GetUpdatedFiles $SourceFolder $DateParam end -------------------------------------------"

